package com.smasher.domain.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.smasher.core.log.Logger
import com.smasher.domain.entity.BeanDomain
import com.smasher.domain.room.DBManager
import com.smasher.domain.room.DomainDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DomainViewModel : ViewModel() {

    private val domainLive = MutableLiveData<List<BeanDomain>>()
    val domainList: LiveData<List<BeanDomain>> = domainLive


    fun setDomainLive(list: List<BeanDomain>) {
        domainLive.value = list
    }


    private fun getDomainDao(): DomainDao {
        return DBManager.db.domainDao()
    }


    fun saveList(list: List<BeanDomain>) {
        viewModelScope.launch(Dispatchers.IO) {
            Logger.e("save data in scope")

            val dao = getDomainDao()
            val result = try {
                Result.success(dao.insertKotlinDomains(list))
            } catch (error: Exception) {
                Result.failure(error)
            }
            Logger.i("saveList success: " + result.isSuccess)
            val sList = dao.loadKotlinBeanDomains()
            withContext(Dispatchers.Main) {
                domainLive.value = sList
            }

        }
    }

    fun saveSingle(item: BeanDomain, callback: (Boolean) -> Unit) {
        viewModelScope.launch(Dispatchers.IO) {
            val dao = getDomainDao()
            val result = try {
                Result.success(dao.insertKotlinDomains(item))
            } catch (error: Exception) {
                Result.failure(error)
            }
            if (result.isSuccess) {
                val list = dao.loadKotlinBeanDomains()
                withContext(Dispatchers.Main) {
                    domainLive.value = list
                }
            }
            withContext(Dispatchers.Main) {
                callback.invoke(result.isSuccess)
            }
        }

    }

    fun deleteSingle(domain: BeanDomain, callback: (Boolean) -> Unit) {
        viewModelScope.launch(Dispatchers.IO) {
            val dao = getDomainDao()
            val result = try {
                Result.success(dao.deleteKotlinDomains(domain))
            } catch (error: Exception) {
                Result.failure(error)
            }
            withContext(Dispatchers.Main) {
                callback.invoke(result.isSuccess)
            }
        }
    }

    fun queryList() {
        viewModelScope.launch(Dispatchers.IO) {
            val dao = getDomainDao()
            val list = dao.loadKotlinBeanDomains()
            withContext(Dispatchers.Main) {
                domainLive.value = list
            }
        }
    }

    fun queryDomain(id: Int) {
        viewModelScope.launch(Dispatchers.IO) {
            val dao = getDomainDao()
            val domain = dao.loadKotlinBeanDomainById(id)
        }
    }

}